package com.sakura.util;

import com.sakura.dbo.ElderlyDBO;
import com.sakura.entity.CheckProject;
import com.sakura.entity.CheckRecord;
import com.sakura.service.CheckProjectService;
import com.sakura.service.CheckRecordService;
import com.sakura.service.ElderlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * @author : Sakura
 * @Date : 2023/11/23 9:27
 * @Description : TODO 定时任务，每天检查有哪些病人需要检查身体
 */
@Component
public class ScheduleBean {

    @Autowired
    private ElderlyService elderlyService;

    @Autowired
    private CheckRecordService checkRecordService;

    @Autowired
    private CheckProjectService checkProjectService;
    /**
     * 查询每天有哪些老人需要重新检查，将需要检查的老人项目重新加入项目计划表中，每天6点更新数据
     * @param :
     * @return void
     * @author Sakura
     * @date 2023/11/23 9:31
     */
    @Scheduled(cron = "0 0 6 * * *")
    public void checkElderlyProjectTask() {

        System.out.println("执行一次");
        //1、查出所有需要检查的老人
        List<ElderlyDBO> needCheckElderlyList = elderlyService.getNeedCheckElderlyList();

        //2、项目表中的每一项内容都加入记录表中，哪些老人需要检测哪些项目
        List<CheckProject> projectList = checkProjectService.list();
        List<CheckRecord> checkRecords = new ArrayList<>();
        //哪一位老人
        for (ElderlyDBO needElderly : needCheckElderlyList) {
            //哪一个项目
            for (CheckProject checkProject : projectList) {
                CheckRecord checkRecord = CheckRecord.builder()
                        .elderlyId(needElderly.getId())
                        .projectId(checkProject.getId())
                        .checkUser(needElderly.getGridId())
                        .build();

                checkRecords.add(checkRecord);
            }
        }
        //3、将记录保存到记录表中
        checkRecordService.saveBatch(checkRecords);
    }
}
